<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class IndexController extends CommonController {
    public function index(){
		//常用列表
		$section_lists=sectionlist();//部门列表
		$unit_lists=unitlist();//单位列表
		$personnel_lists=personnellist();//员工列表
		$warehouse_lists=depot_list();//仓库列表
		$storage_lists=storage_list();//库位列表
		$coin_lists=coinlist();//币制列表
		$country_lists=countrylist();//国家列表
		$unit_lists=unitlist();//单位列表
		$contactscompany_lists=contactsCompanylist();//往来单位列表
		
		$this->assign('section_lists',json_encode($section_lists));
		$this->assign('unit_lists',json_encode($unit_lists));
		$this->assign('personnel_lists',json_encode($personnel_lists));
		$this->assign('warehouse_lists',json_encode($warehouse_lists));
		$this->assign('storage_lists',json_encode($storage_lists));
		$this->assign('coin_lists',json_encode($coin_lists));
		$this->assign('country_lists',json_encode($country_lists));
		$this->assign('unit_lists',json_encode($unit_lists));
		$this->assign('contactscompany_lists',json_encode($contactscompany_lists));
		
		$gl=M('GoodsLog',C('DB_PRS'),C('DB_DEPOT'));
		$time=time()-3600*24;//昨天的现在时间
		$alert_storage_amount=C('base_config.alert_storage_amount');//预计库存
		$checkstock_count=$gl->where(array('add_time'=>array('GT',$time),'storage_amount'=>array('ELT',$alert_storage_amount)))->count();//需盘库数量
		
		$this->assign('checkstock_count',$checkstock_count);
		
		$wr=M('Warehouse');
		$map_wr['warehouse_code']=session('warehouse_code');
		$warehouse_cate=$wr->where($map_wr)->getField('warehouse_cate');
		// dump(session());
		$this->assign('warehouse_cate',$warehouse_cate);
        $this->display();
    }
	//欢迎页面
	public function welcome(){
		
		if(session('warehouse_status')==1){
			$this->redirect('welcome_index');
		}
		if(session('warehouse_status')==2){
			$this->redirect('welcome_vr');
		}
	}
	//欢迎页面(物理仓)
	public function welcome_index(){
		$m=M('ArrivalOrderPlan',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$a=M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'));
		$b=M('OtherInbound',C('DB_PRS'),C('DB_DEPOT'));
		$c=M('OtherOutbound',C('DB_PRS'),C('DB_DEPOT'));
		$d=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
		$e=M('PackageMaterialOrder',C('DB_PRS'),C('DB_DEPOT'));
		$f=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
		$count1=$m->where(array('status'=>0))->count();//未审核入库单
		$count2=$n->where(array('grounding_status'=>0))->count();//待上架商品
		$count3=$n->where(array('inbound_status'=>0,'status'=>1))->count();//待入库商品
		$count4=$a->where(array('status'=>0))->count();//待审核调拨单
		$count5=$b->where(array('status'=>0))->count();//待审核其它入库单
		$count6=$c->where(array('status'=>0))->count();//待审核其它出库单
		$count7=$d->where(array('status'=>0))->count();//待审核生产计划单
		$count8=$e->where(array('status'=>0))->count();//待审核耗材采购单
		$count9=$e->where(array('status'=>1,'inbound_status'=>0))->count();//待入库耗材
		$count10=$f->where(array('is_delivery'=>0))->count();//待出库（发货）订单
		
		
		$count11=$m->where(array('plan_apply_status'=>0))->count();//待入库申报
		$count12=$m->where(array('receive_apply_status'=>0))->count();//待实收申报
		$count13=$m->where(array('plan_print_status'=>0))->count();//待打印监盘计划
		$count14=$m->where(array('print_status'=>0))->count();//待打印理货报告
		
		$map=array();
		$map['pick_status']=0;//未生成波次单
		$map['is_delivery']=0;//未出库
		// $map['transport_cate']=array('not in','1,2,3');//非自提、非第三方物流、非送货
		// $map['is_emergency']=1;
		
		$count15=$f->where($map)->count();//待拣货订单
		
		$map=array();
		$map['pick_status']=1;
		$map['is_delivery']=0;//未出库
		// $map['transport_cate']=array('not in','1,2,3');//非自提、非第三方物流、非送货
		// $map['is_emergency']=1;
		
		$count16=$f->where($map)->where(array('times'=>0))->count();//待打印面单
		// $count17=$f->where(array('plan_apply_status'=>0))->count();//待入库申报
		// $count18=$f->where(array('plan_apply_status'=>0))->count();//待入库申报
		
		// SELECT count(*),FROM_UNIXTIME(add_time,"%Y-%m-%d") time FROM erp_pushorder GROUP BY time
		$f_list_d=$f->field('count(*) count_order,FROM_UNIXTIME(add_time,"%Y-%m-%d") time')->group('time')->having('time>=from_unixtime(unix_timestamp(now())-3600*24*30,"%Y-%m-%d")')->select();
		// $f_list_m=$f->field('count(*) count_order,FROM_UNIXTIME(add_time,"%Y-%m") time')->group('time')->having('time=date_format(now(),"%Y-%m")')->select();
		// echo $f->_sql();
		$day_count='';
		foreach($f_list_d as $k=>$v){
			$day_list.=",'".$v['time']."'";
			$day_count.=','.$v['count_order'];
		}
		$day_list=substr($day_list,1);
		$day_count=substr($day_count,1);
		$this->assign('day_list',$day_list);
		$this->assign('day_count',$day_count);
		
		$this->assign('count1',$count1);
		$this->assign('count2',$count2);
		$this->assign('count3',$count3);
		$this->assign('count4',$count4);
		$this->assign('count5',$count5);
		$this->assign('count6',$count6);
		$this->assign('count7',$count7);
		$this->assign('count8',$count8);
		$this->assign('count9',$count9);
		$this->assign('count10',$count10);
		$this->assign('count11',$count11);
		$this->assign('count12',$count12);
		$this->assign('count13',$count13);
		$this->assign('count14',$count14);
		$this->assign('count15',$count15);
		$this->assign('count16',$count16);
		// $this->assign('count17',$count17);
		// $this->assign('count18',$count18);
		
        $this->display();
    }
	//欢迎页面(虚拟仓)
	//订单统计
	public function welcome_vr(){
		// dump(I());
		$m=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));

		$param = I('param.','','trim');
		
		$start_time=$param['start_time'];
		$end_time=$param['end_time'];
		
		$goods_no=$param['goods_no'];
		$goods_name=$param['goods_name'];
		$come_from=$param['come_from'];
		$merchant_code=$param['merchant_code'];
		
		if(!empty($goods_no)){
			$where['goods_no']=array('like','%$goods_no%');
		}
		if(!empty($goods_name)){
			$where['goods_name']=array('like','%$goods_name%');
		}
		if(!empty($come_from)){
			$where['come_from']=$come_from;
		}
		if(!empty($merchant_code)){
			$where['left(goods_no,10)']=array('like','%merchant_code%');//sql截取左边10位即商家编码
		}
		
		if($start_time!='' && $end_time==''){
			$start_time=strtotime($start_time);
			$end_time=strtotime(date('Y-m-d',time()+86400));
        }else if($start_time=='' && $end_time!=''){
			$end_time=strtotime($end_time);
			$start_time=strtotime(date('Y-m-d',time()));
        }else if($start_time=='' && $end_time=='') {
			$start_time=strtotime(date('Y-m-d',time()));
			$end_time=strtotime(date('Y-m-d',time()+86400));
		}else if($start_time!='' && $end_time!='') {
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time);
		}

		$map['add_time']= array(array('gt',$start_time),array('lt',$end_time));
		
		$nums_month=getMonthNum(date('Y-m-d',$start_time),date('Y-m-d',$end_time));
		
		if($end_time-$start_time<=86400){//一天内
			//按小时统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m-%d %H:00:00') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m-%d %H')")->select();//时间列表
		}
		
		if($end_time-$start_time>86400 and $nums_month<=1){//一个月内
			//按天统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m-%d') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m-%d')")->select();//时间列表
		}
		
		if($nums_month>1 && $nums_month<=12){//一年内
			//按月统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%c') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m')")->select();//时间列表
		}
		// echo $m->getlastsql();
		// die;
		$count_all=0;
			
		if($end_time-$start_time<=86400){//一天内
			$categories="";
			$datas="";
			$categories=',"00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00","08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00","24:00"';
			$cate_list=explode(',',substr($categories,1));
			foreach($cate_list as $ks=>$vs){
				$ii=0;
				foreach($list as $k=>$v){
					$ii++;
					// dump($ii);
					// dump(count($list));
					// dump($ii==count($list));
					// dump($v['t_time']);
					// dump(intval(date("H",strtotime($v['t_time']))));
					if($ks==intval(date("H",strtotime($v['t_time'])))){
						$datas.=','.$v['t_count'];
						$count_all+=$v['t_count'];
						break;
					}
					if($ii==count($list)){
						$datas.=',0';
					}
				}
			}
		}

		if($end_time-$start_time>86400 and $nums_month<=1){//一个月内
			$categories="";
			$datas="";
			$days=($end_time-$start_time)/86400+1;//相差天数
			// dump($days);
			for($i=0;$i<$days;$i++){
				$categories.=',"'.date('Y-m-d',$start_time).'"';
				$ii=0;
				foreach($list as $k=>$v){
					$ii++;
					// dump(date('Y-m-d',$start_time)==$v['t_time']);
					// dump(date('Y-m-d',$start_time));
					// dump($ii==count($list));
					if(date('Y-m-d',$start_time)==$v['t_time']){
						$datas.=','.$v['t_count'];
						$count_all+=$v['t_count'];
						break;
					}
					if($ii==count($list)){
						$datas.=',0';
					}
				}
				$start_time+=86400;
			}
		}
		
		if($nums_month>1 && $nums_month<=12){//一年内
			$categories="";
			$datas="";
			$t_month=intval(date('m',$start_time));//开始月份
			$t_year=intval(date('Y',$start_time));//开始月份
			$datename=date('Y-'.$t_month,$start_time);//开始日期
			$next_month=0;//下一年开始月份
			for($i=0;$i<$nums_month+1;$i++){
				$categories.=',"'.$datename.'"';
				$ii=0;
				foreach($list as $k=>$v){
					$ii++;
					if($datename==$v['t_time']){
						$datas.=','.$v['t_count'];
						$count_all+=$v['t_count'];
						break;
					}
					if($ii==count($list)){//如果找不到匹配时间（即查询结果中无结果，则返回值为0）
						$datas.=',0';
					}
				}
				if($t_month+$i<12){
					$datename=date('Y-'.($t_month+$i+1),$start_time);//更新下月时间
					
				}else{
					$next_month++;
					$datename=($t_year+1).'-'.$next_month;//更新下月时间
				}
			}
		}
		
		if($nums_month>12){//大于1年
			$this->error('查询时间不能大于一年！');
		}
		
		// dump($categories);
		// dump($datas);
		
		// dump($datas);
		// dump($categories);
		// echo $m->getlastsql();
		$pushorder_list=$m->limit(0,15)->select();
		$this->assign('pushorder_list',$pushorder_list);
		
		$this->assign('count_all',$count_all);
		$this->assign('categories',substr($categories,1));
		$this->assign('datas',substr($datas,1));
		$this->assign('list_t',$list);
		$this->display();
    }
    //退出
	public function logout(){
		unset($_SESSION['username']);
		unset($_SESSION['uid']);
		unset($_SESSION['gid']);
		unset($_SESSION['gname']);
		$return=array();
		$return['message']='退出成功！';
		$return['status']=true;
		$this->ajaxReturn($return);
	}

}